/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.statemachine.editpart;

import java.util.Iterator;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.editparts.AbstractEditPart;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Region;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.core.figures.StringFigure;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;


/**
 * 
 */
public class NodeNameDisplayEditPart extends com.cea.papyrus.core.editpart.NodeNameDisplayEditPart{
	
	/**
	 * Create a FeatureList EditPart accepting specified type from drag and drop.
	 * Create a ListEditPart for a List accepting only specified element.
	 * 
	 * @param element 
	 * @param modelManager 
	 * @param elementType 	 * @param graphnode 
	 */
	public NodeNameDisplayEditPart(GraphNode graphnode,org.eclipse.uml2.uml.Element element,ModelManager modelManager) {
		super(graphnode,modelManager);
		Debug.debug(this,"create NameDisplayEditPart("+ element+")",channel);
		this.element = element;
	}

	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		StringFigure f = (StringFigure)getFigure();

		String stereotypePresentation = null;

		GraphNode semanticParent= (GraphNode)getGraphNode().getSemanticParent();
		if(semanticParent!=null){
			if(semanticParent.getSemanticModel() != null) {
				if(semanticParent.getSemanticModel().getPresentation() != null) {
					stereotypePresentation = semanticParent.getSemanticModel().getPresentation();
				} else {
					semanticParent.getSemanticModel().setPresentation(ModelerHelper.TEXT_STEREOTYPE_PRESENTATION);
					stereotypePresentation = ModelerHelper.TEXT_STEREOTYPE_PRESENTATION;
				}
			}

			String stereotypes = "";
			if(!(element instanceof Region)){
				Iterator it = getParent().getChildren().iterator();
				boolean found = false;
				GraphNodeWithSemanticEditPart semParentEP = null;
				while(!found && it.hasNext()){
					AbstractEditPart ep = (AbstractEditPart)it.next();
					if((ep instanceof GraphNodeWithSemanticEditPart) && ((GraphNodeWithSemanticEditPart)ep).getGraphNode().equals(semanticParent)){
						semParentEP = (GraphNodeWithSemanticEditPart)ep;
						found = true;
					}
				}
				if(found)
					stereotypes = semParentEP.verticalStereotypesToDisplay();
			}
			else
				stereotypes = ((GraphNodeWithSemanticEditPart)getParent()).verticalStereotypesToDisplay();
			
			if((stereotypes != "") && 
					((stereotypePresentation != null) && (stereotypePresentation.equals(ModelerHelper.TEXT_ICON_STEREOTYPE_PRESENTATION) 
							|| stereotypePresentation.equals(ModelerHelper.TEXT_STEREOTYPE_PRESENTATION)))){
				f.setStereotypes(stereotypes);
				if(isVisible()){
					if(element instanceof NamedElement){
						f.setName("\n"+((NamedElement)element).getName());
					}
				}
				else
					f.setName("");
				f.setVisible(true);
			}
			else {
				f.setStereotypes(null);
				if(isVisible()){
					if(element instanceof NamedElement){
						f.setName(((NamedElement)element).getName());
					}
				}
				else
					f.setName("");
				f.setVisible(isVisible());
			}

			Rectangle r = new Rectangle(getGraphNode().getPosition(), f.getPreferredSize());
			((GraphicalEditPart) getParent()).setLayoutConstraint(this, f, r);
		}
	}
}
